package demo04_bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created by ztt on 2017/9/16.
 */
@Component
public class StudentService {

    public void read(){
        System.out.println("阅读…………");
        getBook().content();
    }

    public void random(){
        System.out.println("阅读…………");
        randomRead().content();
    }

//    @Bean()/** id默认为方法名 **/
    @Bean(name="book")/** 指定bean的id为book **/
    public Book getBook(){
        return new EnBook();
    }

    @Bean
    public Book randomRead(){
        int choice=(int)Math.floor(Math.random()*4);
        if(choice>2){
            return new EnBook();
        }else{
            return new CnBook();
        }
    }
}

@Component
interface Book{
    void content();
}

@Component
class EnBook implements Book{

    @Autowired
    private BookDao dao;

    public void content(){
        System.out.printf("%s","这是英文书的内容！"+dao);
    }
}

@Component
class CnBook implements Book{

    @Autowired
    private BookDao dao;

    public void content(){
        System.out.printf("%s","这是中文书的内容！"+dao);
    }
}
@Component
class BookDao{

    public String get(){
        return "这是书的内容："+Math.random();
    }

}
